from datetime import datetime

from sqlalchemy.orm import Session

from core.entity.do.models import CmsAlertMessage
from core.entity.vo.view_models import CmsAlertMessageVO, CmsAlertMessageQueryPage


class CmsAlertMessageDao:
    """
    高德终端dao
    """

    @classmethod
    def get_cms_alert_message_by_id(cls, db: Session, id: int):
        """
        根据id查询高德终端信息
        :param db: orm对象
        :param id: 主键ID
        :return: 高德终端信息对象
        """
        cms_alert_message = db.query(CmsAlertMessage) \
            .filter(CmsAlertMessage.id == id) \
            .first()

        return cms_alert_message

    @classmethod
    def get_cms_alert_message(cls, db: Session, cms_alert_message_query: CmsAlertMessageQueryPage):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param fence_id: 终端id
        :param car_number: 车辆编码
        :param begin_time: 开始时间
        :param end_time: 结束时间
        :return: 高德终端信息对象
        """
        alert_type = cms_alert_message_query.alert_type
        car_number = cms_alert_message_query.car_number
        begin_time = cms_alert_message_query.begin_time
        end_time = cms_alert_message_query.end_time
        page_num = cms_alert_message_query.page_num
        page_size = cms_alert_message_query.page_size
        skip = (page_num - 1) * page_size  # 计算跳过的记录数
        limit = page_size  # 每页返回的记录数
        cms_alert_message = db.query(CmsAlertMessage) \
            .filter(CmsAlertMessage.alert_type == alert_type if alert_type else True) \
            .filter(CmsAlertMessage.car_number == car_number if car_number else True) \
            .filter(begin_time.strftime('%Y-%m-%d %H:%M:%S') <= CmsAlertMessage.create_time if begin_time else True) \
            .filter(end_time.strftime('%Y-%m-%d %H:%M:%S') >= CmsAlertMessage.create_time if end_time else True) \
            .offset(skip).limit(limit) \
            .all()

        return cms_alert_message

    @classmethod
    def get_cms_alert_message_total(cls, db, cms_alert_message_query: CmsAlertMessageQueryPage):
        alert_type = cms_alert_message_query.alert_type
        car_number = cms_alert_message_query.car_number
        begin_time = cms_alert_message_query.begin_time
        end_time = cms_alert_message_query.end_time
        return db.query(CmsAlertMessage) \
            .filter(CmsAlertMessage.alert_type == alert_type if alert_type else True) \
            .filter(CmsAlertMessage.car_number == car_number if car_number else True) \
            .filter(begin_time.strftime('%Y-%m-%d %H:%M:%S') <= CmsAlertMessage.create_time if begin_time else True) \
            .filter(end_time.strftime('%Y-%m-%d %H:%M:%S') >= CmsAlertMessage.create_time if end_time else True) \
            .count()

    @classmethod
    def add_cms_alert_message_dao(cls, db: Session, cms_alert_message: CmsAlertMessageVO):
        """
        新增高德终端
        :param db: orm对象
        :param cms_alert_message: 高德终端对象
        :return:
        """
        model_dict = CmsAlertMessage(**cms_alert_message.dict())
        db.add(model_dict)
        db.flush()

        return model_dict

    @classmethod
    def edit_cms_alert_message_dao(cls, db: Session, cms_alert_message: dict):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param cms_alert_message: 需要更新的参数配置字典
        :return:
        """
        db.query(CmsAlertMessage) \
            .filter(CmsAlertMessage.id == cms_alert_message.get('id')) \
            .update(cms_alert_message)

    @classmethod
    def delete_cms_alert_message_dao(cls, db: Session, id: int):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param id: id
        :return:
        """
        db.query(CmsAlertMessage) \
            .filter(CmsAlertMessage.id == id) \
            .delete()
